package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.data.model.ChargeMoneyDetails;
import com.chain.BrotherExchange.data.model.CoinAssetsDetails;
import com.chain.BrotherExchange.data.model.ExtractMoneyDetails;
import com.chain.BrotherExchange.data.model.LegalTenderAssetsDetails;
import com.chain.BrotherExchange.data.model.OtherDetails;
import com.chain.BrotherExchange.data.model.TransferDetailsList;
import com.chain.BrotherExchange.utils.Androids;

import java.util.List;

/**
 * 资产明细
 */
public class AssetsDetailsViewModel extends AndroidViewModel {

    private int currentPage = 1;
    private boolean mIsInitialLoad = true;
    private final int uid = Androids.LocalConfig.getUid();
    private final String token = Androids.LocalConfig.getToken();

    private MutableLiveData<Resource<BaseResponseModel>> mCancelExtractMoneyListData;

    private MutableLiveData<Resource<List<OtherDetails.DataBean.ListBean.ItemsBean>>>
            mOtherDetailsLiveData;
    private MutableLiveData<Resource<List<OtherDetails.DataBean.ListBean.ItemsBean>>>
            mMoreOtherDetailsLiveData;

    private MutableLiveData<Resource<List<CoinAssetsDetails.DataBean.ListBean.ItemsBean>>>
            mCoinAssetsDetailsLiveData;
    private MutableLiveData<Resource<List<CoinAssetsDetails.DataBean.ListBean.ItemsBean>>>
            mMoreCoinAssetsDetailsLiveData;

    private MutableLiveData<Resource<List<ChargeMoneyDetails.DataBean.ListBean.ItemsBean>>>
            mChargeMoneyDetailsLiveData;
    private MutableLiveData<Resource<List<ChargeMoneyDetails.DataBean.ListBean.ItemsBean>>>
            mMoreChargeMoneyDetailsLiveData;

    private MutableLiveData<Resource<List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean>>>
            mExtractMoneyDetailsLiveData;
    private MutableLiveData<Resource<List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean>>>
            mMoreExtractMoneyDetailsLiveData;

    private MutableLiveData<Resource<List<TransferDetailsList.DataBean.ListBean.ItemsBean>>>
            mTransferDetailsLiveData;
    private MutableLiveData<Resource<List<TransferDetailsList.DataBean.ListBean.ItemsBean>>>
            mMoreTransferDetailsLiveData;

    private MutableLiveData<Resource<List<LegalTenderAssetsDetails.DataBean.ListBean>>>
            mLegalTenderAssetsListLiveData;
    private MutableLiveData<Resource<List<LegalTenderAssetsDetails.DataBean.ListBean>>>
            mMoreLegalTenderAssetsListLiveData;


    public AssetsDetailsViewModel(@NonNull Application application) {
        super(application);
        mCancelExtractMoneyListData = new MutableLiveData<>();

        mCoinAssetsDetailsLiveData = new MutableLiveData<>();
        mMoreCoinAssetsDetailsLiveData = new MutableLiveData<>();

        mChargeMoneyDetailsLiveData = new MutableLiveData<>();
        mMoreChargeMoneyDetailsLiveData = new MutableLiveData<>();

        mExtractMoneyDetailsLiveData = new MutableLiveData<>();
        mMoreExtractMoneyDetailsLiveData = new MutableLiveData<>();

        mTransferDetailsLiveData = new MutableLiveData<>();
        mMoreTransferDetailsLiveData = new MutableLiveData<>();

        mOtherDetailsLiveData = new MutableLiveData<>();
        mMoreOtherDetailsLiveData = new MutableLiveData<>();

        mLegalTenderAssetsListLiveData = new MutableLiveData<>();
        mMoreLegalTenderAssetsListLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<BaseResponseModel>> cancelExtractMoney(int id) {
        DataRepository.get().cancelExtractMoney(token, uid, id, result -> {
            if (result.isSucceed()) {
                mCancelExtractMoneyListData.setValue(Resource.success(result.getMsg()));
            } else {
                mCancelExtractMoneyListData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mCancelExtractMoneyListData;
    }

    /**
     * 请求新法币资产详情
     */
    public MutableLiveData<Resource<List<LegalTenderAssetsDetails.DataBean.ListBean>>>
    newLegalTenderAssetsDetails(){
        currentPage = 1;
        legalTenderDetails(mLegalTenderAssetsListLiveData, currentPage);
        return mLegalTenderAssetsListLiveData;
    }

    /**
     * 请求更多法币资产详情
     */
    public MutableLiveData<Resource<List<LegalTenderAssetsDetails.DataBean.ListBean>>>
    moreLegalTenderAssetsDetails(){
        legalTenderDetails(mMoreLegalTenderAssetsListLiveData, ++currentPage);
        return mMoreLegalTenderAssetsListLiveData;
    }

    /**
     * 请求新的币币资产明细
     */
    public MutableLiveData<Resource<List<CoinAssetsDetails.DataBean.ListBean.ItemsBean>>>
    newCoinAssetsDetails() {
        currentPage = 1;
        coinAssetsDetails(mCoinAssetsDetailsLiveData, currentPage);
        return mCoinAssetsDetailsLiveData;
    }

    /**
     * 请求更多币币资产明细
     */
    public MutableLiveData<Resource<List<CoinAssetsDetails.DataBean.ListBean.ItemsBean>>>
    moreCoinAssetsDetails() {
        coinAssetsDetails(mMoreCoinAssetsDetailsLiveData, ++currentPage);
        return mMoreCoinAssetsDetailsLiveData;
    }

    /**
     * 请求新的充币明细
     */
    public MutableLiveData<Resource<List<ChargeMoneyDetails.DataBean.ListBean.ItemsBean>>>
    newChargeMoneyDetails() {
        currentPage = 1;
        chargeMoneyDetails(mChargeMoneyDetailsLiveData, currentPage);
        return mChargeMoneyDetailsLiveData;
    }

    /**
     * 请求更多充币明细
     */
    public MutableLiveData<Resource<List<ChargeMoneyDetails.DataBean.ListBean.ItemsBean>>>
    moreChargeMoneyDetails() {
        chargeMoneyDetails(mMoreChargeMoneyDetailsLiveData, ++currentPage);
        return mMoreChargeMoneyDetailsLiveData;
    }

    /**
     * 请求新的提币明细
     */
    public MutableLiveData<Resource<List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean>>>
    newExtractMoneyDetails() {
        currentPage = 1;
        extractMoneyDetails(mExtractMoneyDetailsLiveData, currentPage);
        return mExtractMoneyDetailsLiveData;
    }

    /**
     * 请求更多提币明细
     */
    public MutableLiveData<Resource<List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean>>>
    moreExtractMoneyDetails() {
        extractMoneyDetails(mMoreExtractMoneyDetailsLiveData, ++currentPage);
        return mMoreExtractMoneyDetailsLiveData;
    }

    /**
     * 请求新的转账明细
     */
    public MutableLiveData<Resource<List<TransferDetailsList.DataBean.ListBean.ItemsBean>>>
    newTransferDetails() {
        currentPage = 1;
        transferDetails(mTransferDetailsLiveData, currentPage);
        return mTransferDetailsLiveData;
    }

    /**
     * 请求更多转账明细
     */
    public MutableLiveData<Resource<List<TransferDetailsList.DataBean.ListBean.ItemsBean>>>
    moreTransferDetails() {
        transferDetails(mMoreTransferDetailsLiveData, ++currentPage);
        return mMoreTransferDetailsLiveData;
    }

    /**
     * 请求新的其他明细
     */
    public MutableLiveData<Resource<List<OtherDetails.DataBean.ListBean.ItemsBean>>>
    newOtherDetails() {
        currentPage = 1;
        otherDetails(mOtherDetailsLiveData, currentPage);
        return mOtherDetailsLiveData;
    }

    /**
     * 请求更多其他明细
     */
    public MutableLiveData<Resource<List<OtherDetails.DataBean.ListBean.ItemsBean>>>
    moreOtherDetails() {
        otherDetails(mMoreOtherDetailsLiveData, ++currentPage);
        return mMoreOtherDetailsLiveData;
    }

    /**
     * 币币明细
     */
    private void coinAssetsDetails(
            MutableLiveData<Resource<List<CoinAssetsDetails.DataBean.ListBean.ItemsBean>>> liveData, int page) {
        if (mIsInitialLoad) {
            liveData.setValue(Resource.loading());
        }

        DataRepository.get().coinAssetsDetailsList(uid, token, page, result -> {
            CoinAssetsDetails.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                CoinAssetsDetails.DataBean.ListBean list = resultData.getList();
                List<CoinAssetsDetails.DataBean.ListBean.ItemsBean> items = list.getItems();
                if (items != null && !items.isEmpty()) {
                    mIsInitialLoad = false;
                    liveData.setValue(Resource.success(items));
                } else {
                    liveData.setValue(Resource.empty());
                }
            } else {
                loadState(liveData, result);
            }
        });
    }


    /**
     * 充币明细
     */
    private void chargeMoneyDetails(
            MutableLiveData<Resource<List<ChargeMoneyDetails.DataBean.ListBean.ItemsBean>>> liveData, int page) {
        if (mIsInitialLoad) {
            liveData.setValue(Resource.loading());
        }
        DataRepository.get().chargeMoneyDetailsList(uid, token, page, result -> {
            ChargeMoneyDetails.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                ChargeMoneyDetails.DataBean.ListBean list = resultData.getList();
                List<ChargeMoneyDetails.DataBean.ListBean.ItemsBean> items = list.getItems();
                if (items != null && !items.isEmpty()) {
                    mIsInitialLoad = false;
                    liveData.setValue(Resource.success(items));
                } else {
                    liveData.setValue(Resource.empty());
                }
            } else {
                loadState(liveData, result);
            }
        });
    }

    /**
     * 提币明细
     */
    private void extractMoneyDetails(
            MutableLiveData<Resource<List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean>>> liveData, int page) {
        if (mIsInitialLoad) {
            liveData.setValue(Resource.loading());
        }
        DataRepository.get().extractMoneyDetailsList(uid, token, page, result -> {
            ExtractMoneyDetails.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                ExtractMoneyDetails.DataBean.ListBean list = resultData.getList();
                List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean> items = list.getItems();
                if (items != null && !items.isEmpty()) {
                    mIsInitialLoad = false;
                    liveData.setValue(Resource.success(items));
                } else {
                    liveData.setValue(Resource.empty());
                }
            } else {
                loadState(liveData, result);
            }
        });
    }

    /**
     * 转账明细
     */
    private void transferDetails(
            MutableLiveData<Resource<List<TransferDetailsList.DataBean.ListBean.ItemsBean>>> liveData, int page) {
        if (mIsInitialLoad) {
            liveData.setValue(Resource.loading());
        }
        DataRepository.get().transferDetailsList(uid, token, page, result -> {
            TransferDetailsList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                TransferDetailsList.DataBean.ListBean list = resultData.getList();
                List<TransferDetailsList.DataBean.ListBean.ItemsBean> items = list.getItems();
                if (items != null && !items.isEmpty()) {
                    mIsInitialLoad = false;
                    liveData.setValue(Resource.success(items));
                } else {
                    liveData.setValue(Resource.empty());
                }
            } else {
                loadState(liveData, result);
            }
        });
    }

    /**
     * 其他明细
     */
    private void otherDetails(
            MutableLiveData<Resource<List<OtherDetails.DataBean.ListBean.ItemsBean>>> liveData, int page) {
        if (mIsInitialLoad) {
            liveData.setValue(Resource.loading());
        }
        DataRepository.get().otherDetailsList(uid, token, page, result -> {
            OtherDetails.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                OtherDetails.DataBean.ListBean list = resultData.getList();
                List<OtherDetails.DataBean.ListBean.ItemsBean> items = list.getItems();
                if (items != null && !items.isEmpty()) {
                    mIsInitialLoad = false;
                    liveData.setValue(Resource.success(items));
                } else {
                    liveData.setValue(Resource.empty());
                }
            } else {
                loadState(liveData, result);
            }
        });
    }

    /**
     * 法币资产详情
     */
    private void legalTenderDetails(
            MutableLiveData<Resource<List<LegalTenderAssetsDetails.DataBean.ListBean>>> liveData, int page) {
        if (mIsInitialLoad) {
            liveData.setValue(Resource.loading());
        }

        DataRepository.get().legalTenderAssetsDetails(uid, token, page, result -> {
            LegalTenderAssetsDetails.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                List<LegalTenderAssetsDetails.DataBean.ListBean> list = resultData.getList();
                if (list != null && !list.isEmpty()) {
                    mIsInitialLoad = false;
                    liveData.setValue(Resource.success(list));
                } else {
                    liveData.setValue(Resource.empty());
                }
            } else {
                loadState(liveData, result);
            }
        });
    }


    /**
     * 加载状态
     */
    private <T> void loadState(MutableLiveData<Resource<T>> liveData, BaseResponseModel result) {
        if (result.isNotNetwork()) {
            liveData.setValue(Resource.notNetwork());
        } else if (result.isLogonFailure()) {
            liveData.setValue(Resource.tokenError(result.getMsg()));
        } else {
            liveData.setValue(Resource.error(result.getMsg()));
        }
    }

}
